﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace TOOL
{
    /// <summary>
    /// 日志处理
    /// </summary>
    class TLog
    {
        /// <summary>
        /// 成功日志
        /// </summary>
        /// <param name="strContent"></param>
        public static void WriteSucess(string strContent)
        {
            Write(strContent, true);
        }
        /// <summary>
        /// 失败日志
        /// </summary>
        /// <param name="strContent"></param>
        public static void WriteFail(string strContent)
        {
            Write(strContent, false);
        }
        /// <summary>
        /// 执行的SQL语句日志
        /// </summary>
        /// <param name="strContent"></param>
        public static void WriteSQL(string strContent)
        {
            string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string _path = basePath + "\\Log\\Sql";
            if (!Directory.Exists(_path))
            {
                Directory.CreateDirectory(_path);
            }

            string fileName = string.Format("{0}\\{1}.txt", _path, DateTime.Now.ToString("yyyy-MM-dd"));
            if (!File.Exists(fileName))
            {
                FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                long fl = fs.Length;
                fs.Seek(fl, SeekOrigin.End);
                sw.WriteLine(strContent);//开始写入值
                sw.Close();
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                long fl = fs.Length;
                fs.Seek(fl, SeekOrigin.Begin);
                sw.WriteLine(strContent + "\n\n");//开始写入值
                sw.Close();
                fs.Close();
            }
        }
        /// <summary>
        /// 写入文本文件
        /// </summary>
        /// <param name="strContent">写的内容</param>
        private static void Write(string strContent,bool su)
        {
            try
            {
                string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string _path = basePath + (su ? "\\Log\\Sucess" : "\\Log\\Fail");
                if (!Directory.Exists(_path))
                {
                    Directory.CreateDirectory(_path);
                }

                string fileName = string.Format("{0}\\{1}.txt", _path, DateTime.Now.ToString("yyyy-MM-dd"));
                if (!File.Exists(fileName))
                {
                    FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(fs);
                    long fl = fs.Length;
                    fs.Seek(fl, SeekOrigin.End);
                    sw.WriteLine(strContent);//开始写入值
                    sw.Close();
                    fs.Close();
                }
                else
                {
                    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(fs);
                    long fl = fs.Length;
                    fs.Seek(fl, SeekOrigin.Begin);
                    sw.WriteLine(strContent + "\n\n");//开始写入值
                    sw.Close();
                    fs.Close();
                }
            }catch(Exception ex)
            {
                TLog.WriteFail(ex.Message);
            }
        }
    }
}
